package com.erp.auto.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.erp.auto.domain.ErpAutoEvaluationConfig;
import com.erp.auto.domain.ErpAutoInfoModuleConfig;
import com.erp.auto.domain.ErpAutoPulishProductProject;
import com.erp.auto.mapper.ErpAutoEvaluationConfigMapper;
import com.erp.auto.mapper.ErpAutoInfoModuleConfigMapper;
import com.erp.auto.mapper.ErpAutoPulishProductProjectMapper;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.erp.auto.domain.bo.ErpAutoPlanShareBo;
import com.erp.auto.domain.vo.ErpAutoPlanShareVo;
import com.erp.auto.domain.ErpAutoPlanShare;
import com.erp.auto.mapper.ErpAutoPlanShareMapper;
import com.erp.auto.service.IErpAutoPlanShareService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 自动化计划分享Service业务层处理
 *
 * @author WXX
 * @date 2024-03-18
 */
@RequiredArgsConstructor
@Service
public class ErpAutoPlanShareServiceImpl implements IErpAutoPlanShareService {

    private final ErpAutoPlanShareMapper baseMapper;
    private final ErpAutoPulishProductProjectMapper pulishProductProjectMapper;
    private final ErpAutoInfoModuleConfigMapper infoModuleConfigMapper;
    private final ErpAutoEvaluationConfigMapper evaluationConfigMapper;

    /**
     * 查询自动化计划分享
     */
    @Override
    public ErpAutoPlanShareVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询自动化计划分享列表
     */
    @Override
    public TableDataInfo<ErpAutoPlanShareVo> queryPageList(ErpAutoPlanShareBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpAutoPlanShare> lqw = buildQueryWrapper(bo);
        Page<ErpAutoPlanShareVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询自动化计划分享列表
     */
    @Override
    public List<ErpAutoPlanShareVo> queryList(ErpAutoPlanShareBo bo) {
        LambdaQueryWrapper<ErpAutoPlanShare> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpAutoPlanShare> buildQueryWrapper(ErpAutoPlanShareBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpAutoPlanShare> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPlanId() != null, ErpAutoPlanShare::getPlanId, bo.getPlanId());
        lqw.like(StringUtils.isNotBlank(bo.getShareName()), ErpAutoPlanShare::getShareName, bo.getShareName());
        lqw.eq(bo.getPlanType() != null, ErpAutoPlanShare::getPlanType, bo.getPlanType());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpAutoPlanShare::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增自动化计划分享
     */
    @Override
    public R<Object> insertByBo(ErpAutoPlanShareBo bo) {
        ErpAutoPlanShare add = BeanUtil.toBean(bo, ErpAutoPlanShare.class);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            return R.ok(add.getId());
        }
        return R.fail();
    }

    /**
     * 修改自动化计划分享
     */
    @Override
    public Boolean updateByBo(ErpAutoPlanShareBo bo) {
        ErpAutoPlanShare update = BeanUtil.toBean(bo, ErpAutoPlanShare.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpAutoPlanShare entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除自动化计划分享
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R<Object> useShareCode(Long id) {
        ErpAutoPlanShare planShare = baseMapper.selectById(id);
        Integer planType = planShare.getPlanType();
        Long planId = planShare.getPlanId();
        Map<String, Object> map = new HashMap<>();
        map.put("planType", planType);
        map.put("isUse", 1);
        //TODO 分享码待填写完整 下架/涨价降价
        if (planType == 0) {
            // 上架
            ErpAutoPulishProductProject project = pulishProductProjectMapper.selectById(planId);
            if (project == null) {
                return R.fail("找不到计划");
            }
            map.put("config", project);
        }
        else if (planType == 1) {
            // 下架
            
        }
        else if (planType == 4) {
            //信息模块
            ErpAutoInfoModuleConfig infoModuleConfig = infoModuleConfigMapper.selectById(planId);
            if (infoModuleConfig == null) {
                return R.fail("找不到计划");
            }
            map.put("config", infoModuleConfig);
        }
        else if (planType == 5) {
            // 测评
            ErpAutoEvaluationConfig evaluationConfig = evaluationConfigMapper.selectById(planId);
            if (evaluationConfig == null) {
                return R.fail("找不到计划");
            }
            map.put("config", evaluationConfig);
        }
        return R.ok(map);
    }
}
